package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        if (taskCompleteContract == null){
            throw new LogicException("完成运维工单参数为空");
        }

        String innerCode = taskCompleteContract.getInnerCode();
        int taskType = taskCompleteContract.getTaskType();
        //如果是投放工单
        if (taskType == VMSystem.TASK_TYPE_DEPLOY){
            vendingMachineService.updateStatus(innerCode,VMSystem.VM_STATUS_RUNNING);
        }
        //如果是撤机工单
        if (taskType == VMSystem.TASK_TYPE_REVOKE) {
            vendingMachineService.updateStatus(innerCode,VMSystem.VM_STATUS_REVOKE);
        }
    }
}
